import { mapState, mapMutations } from 'vuex'
import { getNewToken,toDate,reverseDate } from '../../config/util'
export default {
	name: 'orderDetail',
	data() {
		return {
			paying:false,//系统正在确认支付
			flights: [],
			dialog: false,
			dialogText: "",
			insurance:false,
		}
	},
	computed: {
		...mapState([
			'orderDetail','token'
		]),
	},
	methods: {
		...mapMutations([
			'RECORD_TOKEN'
		]),
		endorse() {
			this.dialog = true;
			this.dialogText = "进行改期操作"
		},
		refund() {
			this.dialog = true;
			this.dialogText = "进行退票操作"
		},
		close() {
			this.dialog = false;
		},
		pay(order) {
			let url = '/pay/pre?temp=' + Math.random();
			let params = { orderNumber : order.orderNumber }
			let status = order.otherStatus;
			if(status=='waiting_for_process'){
				url = '/pay/endorsePre?temp=' + Math.random(); 
				params = { endorseOrderid : order.orderNumber }
			}
			this.$axios.post(url, params, {
					headers: { 'authorization': this.token }
				})
				.then((res) => {
					this.RECORD_TOKEN({ type: "update", val: getNewToken(res.headers) });
					this.payHandle(res);
				})
				
		},
		cancel(order){
			this.$axios.get('order/cancel/'+order.orderNumber,
					{
						headers: {'authorization':this.token}
					}
				)
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});	
					if(res.data.success){
						this.orderDetail.orderStatus = 5;
						alert("订单取消成功")						
					}else{
						alert("取消订单失败")
					}
				})
		},
		payHandle(res){
			var status = res.data.status;
			if(status==1001) {
				var url = res.data.resultInfo;
				if(url){
					window.location.href = url;
				}						
			}
			else{
				alert("支付失败")
			}
		},
		viewDetail(){
			this.$router.push({ path: '/insuranceDetail' });
		},
		baggage() {
			var flights = this.flights;
			this.$router.push({ path: '/endorse?data='+getData() });
			function getData(){
				var data = {
					airCompany:flights[0][0].airlineNumber.substr(0,2),
					fromAirport:flights[0][0].deptCode,
					toAirport:flights[flights.length-1][flights[flights.length-1].length-1].arrCode,
					cabinInfo:flights[0][0].cabin,
					fromDate:flights[0][0].deptDate,
					lastUpdateTime:null,
					pageNo:1,
					countPerPage:10
				}
				return JSON.stringify(data);
			}
		},
		getInsuranceDetail(){
			this.$axios.get('insurance/insureOrders/'+this.orderDetail.orderNumber, {
					headers: { 'authorization': this.token }
				})
				.then((res) => {
					this.RECORD_TOKEN({ type: "update", val: getNewToken(res.headers) });
					var result = res.data;
					if(JSON.stringify(result) != "{}"){
						this.insurance = true;
					}
				})
				
		},
		//处理行程信息
		handleFlightData() {
			var orderDetail = this.orderDetail;
			var flightSegments = [];
			for(var i = 0; i < orderDetail.list.length; i++) {
				flightSegments.push(orderDetail.list[i].flightSegment);
			}
			var legs = Math.max.apply(Math, flightSegments);
			for(var i = 0; i < legs; i++) {
				this.flights.push([]);
			}
			for(var i = 0; i < orderDetail.list.length; i++) {
				var flightSegment = orderDetail.list[i].flightSegment - 1;
				this.flights[flightSegment].push(orderDetail.list[i]);
			}
		},
		//中转停留时间
		stopTime(seg,index,flight){
			if(flight[index+1]){
				var toDate = seg.arrDate+" "+seg.arrTime;
				var fromDate = flight[index + 1].deptDate+" "+flight[index + 1].deptTime;
				var min = (reverseDate(fromDate) - reverseDate(toDate)) / 60000;
				return min;
			}
				
		}
	},
	created() {
		document.title = "订单详情";
		this.handleFlightData();
		//获取保险详情
		this.getInsuranceDetail();
		console.log(this.flights)
	}
}